$(function () {
    obj.init();
});

var obj = new JsFun();
function JsFun() {
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'Device/getRepairProjectData');
            this.dataGrid.parseToolbar().init().activeBtn(null,$('#bt_list_edit'));
            this.listener();
        },
        listener: function () {
            var $this = this;
            //添加
            $('#bt_list_add').click(function () {
                $this.funObj().addProject();
            });
            //删除
            $('#bt_list_del').click(function () {
                $this.funObj().delProject();
            });
            //修改
            $('#bt_list_edit').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().editProject();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                addProject: function () {
                    window.top.xhTop.showModal({
                        title: '创建设备维修申请单',
                        content: xhTool.getFrameString('Device/createProject', {}, 250),
                        // button: '保存',
                        size: 'sm',
                        width:'600'
                    });
                },
                delProject: function () {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    xhExtra.askChangeStatus('Device/delProject', {ids: ids}, $this.dataGrid, '确认删除？');
                },
                editProject: function () {
                    var id = $this.dataGrid.getSelectValue('iId');
                    var sProjectName = $this.dataGrid.getSelectValue('sProjectName');
                    var money = $this.dataGrid.getSelectValue('sProjectPay');
                    var remark = $this.dataGrid.getSelectValue('sRemark');
                    if (id.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '修改收费项目',
                        html: '<div>' +
                        '维修项目：<input disabled="disabled" id="project" class="swal2-input" value="'+ sProjectName + '">' +
                        '金额：<input id="money" class="swal2-input" value="'+ money + '">' +
                        '备注：<textarea type="text" id="remark" class="swal2-textarea">'+ remark +'</textarea>'+
                        '</div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var pay     = $('#money', window.top.document).val();
                                var sRemark     = $('#remark', window.top.document).val();
                                if(pay < 1){
                                    reject('请填写金额');
                                } else {
                                    xhExtra.ajaxRequest('Device/modifyProject', {
                                        id: id,
                                        pay: pay,
                                        remark: sRemark
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid.reload();
                                }
                            })
                        }
                    })
                }
            }
        }
    }
}